# Contributor: Konstantin Kulikov <k.kulikov2@gmail.com>
# Maintainer: Konstantin Kulikov <k.kulikov2@gmail.com>

# Keep in sync with grafana-frontend.

pkgname=grafana
pkgver=9.0.2
pkgrel=0
_commit=0641b5d0cd # git rev-parse --short HEAD
_stamp=1656415480 # git --no-pager show -s --format=%ct
pkgdesc="Open source, feature rich metrics dashboard and graph editor"
url="https://grafana.com"
# x86, armhf, armv7: many test failures, for example https://github.com/grafana/grafana/issues/26389
# s390x: test failures https://github.com/grafana/grafana/issues/26390
arch="all !s390x !x86 !armhf !armv7"
license="AGPL-3.0-only"
depends="grafana-frontend=$pkgver-r$pkgrel"
makedepends="go"
checkdepends="mailcap"
install="$pkgname.pre-install"
subpackages="$pkgname-openrc"
options="net"
source="$pkgname-$pkgver.tar.gz::https://github.com/grafana/grafana/archive/v$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	fix-dev-dashboards-tests.patch"

export GOPATH="$srcdir"/go
export GOCACHE="$srcdir"/go-build
export GOTMPDIR="$srcdir"
export GOFLAGS="$GOFLAGS -modcacherw"

# secfixes:
#   8.5.3-r0:
#     - CVE-2022-29170
#   8.3.6-r0:
#     - CVE-2022-21702
#     - CVE-2022-21703
#     - CVE-2022-21713
#   8.3.4-r0:
#     - CVE-2022-21673
#   8.3.2-r0:
#     - CVE-2021-43813
#     - CVE-2021-43815
#   8.3.1-r0:
#     - CVE-2021-43798
#   8.2.4-r0:
#     - CVE-2021-41244
#   7.4.5-r0:
#     - CVE-2021-28146
#     - CVE-2021-28147
#     - CVE-2021-28148
#     - CVE-2021-27962
#   7.0.2-r0:
#     - CVE-2020-13379
#   6.3.4-r0:
#     - CVE-2019-15043

prepare() {
	default_prepare
	# XXX: hotfix segfault
	go get -u gonum.org/v1/gonum@v0.11.0
}

build() {
	GOBIN="$(pwd)" go install github.com/google/wire/cmd/wire@v0.5.0
	./wire gen -tags oss ./pkg/server ./pkg/cmd/grafana-cli/runner

	local ldflags="-X main.version=$pkgver -X main.commit=$_commit -X main.buildstamp=$_stamp"
	go build -ldflags "$ldflags" -v ./pkg/cmd/grafana-server
	go build -ldflags "$ldflags" -v ./pkg/cmd/grafana-cli

}

check() {
	local pkgs="./..."

	case "$CARCH" in
	# FP precision bugs in tests.
	aarch64|ppc64le) pkgs="$(go list ./... | grep -Ev 'pkg/tsdb/testdatasource$')" ;;
	esac

	go test $pkgs
}

package() {
	install -Dm755 "$srcdir/$pkgname.initd" "$pkgdir/etc/init.d/$pkgname"
	install -Dm644 "$srcdir/$pkgname.confd" "$pkgdir/etc/conf.d/$pkgname"
	install -Dm755 "$builddir/$pkgname-server" "$pkgdir/usr/sbin/$pkgname-server"
	install -Dm755 "$builddir/$pkgname-cli" "$pkgdir/usr/bin/$pkgname-cli"
	install -Dm644 "$builddir/conf/sample.ini" "$pkgdir/etc/grafana.ini"
	install -dm755 "$pkgdir/usr/share/grafana"
	cp -r "$builddir/conf" "$pkgdir/usr/share/$pkgname/"
}

sha512sums="
bb694d17b7c7f96143718ede8b795d93ba86ade3207c438c8e61838a04c31ff1d5f979259a09d60b223f43aee6975d43b597ddc5d2a88e86b7112e4d963d5c4f  grafana-9.0.2.tar.gz
b0a781e1b1e33741a97e231c761b1200239c6f1235ffbe82311fe883387eb23bef262ad68256ebd6cf87d74298041b53b947ea7a493cfa5aa814b2a1c5181e13  grafana.initd
c2d9896ae9a9425f759a47aeab42b7c43b63328e82670d50185de8c08cda7b8df264c8b105c5c3138b90dd46e86598b16826457eb3b2979a899b3a508cbe4e8c  grafana.confd
de9cbf41fcaffc73422298f6d32fc95358657dcacf9fa6e991e45f472aec894c3be406407ab289615a7cccc14a9014dd654a376470534adff009180389f64fa6  fix-dev-dashboards-tests.patch
"
